# vim:ts=4:sw=4:noet:
""" Dialog Controllers """

from c.ui.wxw.dialogs import GetInfoMethodDialog, GetInfoRobotDialog
from c.compiler.disassemble import *
import c.compiler.spy as spy

class GetInfoMethod:
	def __init__(self, parent, pitem):
		self.dialog = GetInfoMethodDialog(parent=parent, id=-1, title="Method - %s" % pitem.name)
		obj = pitem.obj
		cls = pitem.clsitem.obj
		if pitem.type=="cmethod":
			cls = cls.cls
		self.dialog.SetTitle("Method - %s" % obj.name)
		# name
		self.dialog.lblSignature.SetLabel(obj.name)
		self.dialog.lblInClass.SetLabel(cls.name)
		# bytecodes
		asm, instCount = disassembleMethod(obj.bytecodes, obj.literals)
		self.dialog.lblByteInfo.SetLabel("%d instructions, %d bytes" % (instCount, len(obj.bytecodes)))
		self.dialog.textBytecodes.setText(asm)
		self.dialog.textBytecodes.SetReadOnly(True)
		# literals
		self.dialog.lblLitInfo.SetLabel("%d literals" % len(obj.literals))
		for lit in obj.literals:
			self.dialog.listLiterals.Append(lit.asString())
		# state
		if obj.state==spy.STATE_COMPILED:
			self.dialog.lblState.SetLabel("Compiled")
		elif obj.state==spy.STATE_ERROR:
			self.dialog.lblState.SetLabel("Error")		
		else:
			self.dialog.lblState.SetLabel("Uncompiled")		
		self.dialog.SetMinSize(self.dialog.mainSizer.GetMinSize())
		self.dialog.Fit()
		self.dialog.Centre()
		
	def do(self):
		return self.dialog.ShowModal()
		
class GetInfoRobot:
	def __init__(self, parent, pitem):
		self.dialog = GetInfoRobotDialog(parent, "Robot - %s" % pitem.name)
		
		vdate, vtime, rev = parent.controller.r.getVersion()
		lval = "%s (%s/%s/%s)" % (rev, vdate[4:], vdate[2:4], vdate[:2])
		self.dialog.lblVersion.SetLabel(lval)
		
		plat, arch = parent.controller.r.getPlatform()
		lval = "%s (%s)" % (plat, arch)
		self.dialog.lblPlatform.SetLabel(lval)
		
		mused = parent.controller.latestImageSize
		mtotal = 24*1024
		mval = "Unknown"
		if mused!=-1:
			mval = "%d/%dKb (%d%%)" % (mused/1024, mtotal/1024, mused/float(mtotal)*100.0)
		self.dialog.lblMemory.SetLabel(mval)
		self.dialog.fit()

	def do(self):
		return self.dialog.ShowModal()
